package com.c201801020208.ybl.Company;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.c201801020208.ybl.Company.Activity.AboutActivity;
import com.c201801020208.ybl.Company.Activity.AddJobActivity;
import com.c201801020208.ybl.Login.Activity.Login2Activity;
import com.c201801020208.ybl.R;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMManager;

import static android.content.Context.MODE_PRIVATE;

public class MyCompanyFragment extends Fragment {
    String username = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_company, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RelativeLayout button2 = getActivity().findViewById(R.id.about);
        RelativeLayout button = getActivity().findViewById(R.id.exit);
        RelativeLayout button3 = getActivity().findViewById(R.id.zuzhi);
        TextView textView = getActivity().findViewById(R.id.textView11);
        username = getActivity().getIntent().getStringExtra("username");
        textView.setText(username);

        //发布
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddJobActivity.class);
                intent.putExtra("username", username);
                startActivity(intent);
            }
        });

        //退出
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                V2TIMManager.getInstance().logout(new V2TIMCallback() {
                    @Override
                    public void onError(int code, String desc) {
                        SharedPreferences preferences = getActivity().getSharedPreferences("user", MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.clear();
                        Intent intent = new Intent(getActivity(), Login2Activity.class);
                        startActivity(intent);
                        getActivity().finish();
                    }

                    @Override
                    public void onSuccess() {
                        SharedPreferences preferences = getActivity().getSharedPreferences("user", MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.clear();
                        editor.commit();
                        Intent intent = new Intent(getActivity(), Login2Activity.class);
                        startActivity(intent);
                        getActivity().finish();
                    }
                });
            }
        });
        //关于
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AboutActivity.class);
                startActivity(intent);
            }
        });
    }
}
